<?php

namespace XWAM\Addon\PXCallSystem\Model;

/**
 * QueueModel.class数据库模型
 *
 * @author SoChishun <14507247@qq.com>
 * @version 1.0
 * @since 1.0.0 <2015-4-24> SoChishun Added.
 */
class QueueModel extends AppbaseModel {

    /**
     * 数据表名称
     * @var string
     */
    protected $tableName = 'tcti_acd_queue';

    /**
     * 字段定义
     * @var array
     */
    protected $fields = array();

    /**
     * 命名范围
     * @var array
     */
    protected $_scope = array(
        'export' => array(
            'field'=>"siteID as '租户ID',extension as '队列号',descr as '队列名称',overflowStrategy as '溢出标志',overflowTarget as '溢出目标',members as '座席号码',trunkName as '远程座席中继',music as '队列等待音目录',maxwait as '等待超时时间(秒)',remark as '备注',createdTime as '创建时间'",
            'order' => 'siteID',
            'cache' => true,
        ),
        'item_list' => array(
            'field' => 'id,siteID,extension,descr,overflowStrategy,overflowTarget,members,trunkName,music,maxwait,remark,createdTime',
            'order' => 'siteID',
        ),);

    /**
     * 是否批量验证
     * @var boolean
     */
    protected $patchValidate = true;

    /**
     * 验证规则
     * @var array
     */
    protected $_validate = array();

    /**
     * 自动填充
     * @var array
     */
    protected $_auto = array();

    /**
     * 返回列表
     * @return array
     * @xince 1.0 <2015-6-9> SoChishun Added.
     */
    public function get_queue_list($where) {
        $orderby = I('sort', $this->_scope['item_list']['order']) . ' ' . I('order');
        $page = I('page', 1) . ',' . I('rows', C('USER.PAGE_SIZE'));
        $total = $this->cache(true)->where($where)->count('id');
        $list = $this->cache(true)->scope('item_list')->where($where)->order($orderby)->page($page)->select();
        $out = array();
        foreach ($list as $row) {
            $row['overflowStrategyText'] = get_overflowStrategy_name($row['overflowStrategy']);
            $row['membersText'] = replace_ln(',', $row['members']);
            $row['musicText'] = ltrim($row['music'], 'xcall_');
            $row['siteIDText'] = get_siteID_text($row['siteID']);
            $out[] = $row;
        }
        return array('total' => $total, 'rows' => $out);
    }

}
